﻿using System.Text;

namespace CommonUtils
{
    public class NameUtil
    {
        public static string[] FuXings;
        public static string[] DanXings;
        public static string[] Xings;
        public static string[] Mings;
        public static string[] Villages;

        static NameUtil()
        {
            var fuXingsText = ResourceUtil.GetTextFromCommon("百家姓-复姓.txt");
            FuXings = fuXingsText.RemoveWrappings().SplitByPunctuations();

            var danXingsText = ResourceUtil.GetTextFromCommon("百家姓.txt");
            DanXings = danXingsText.Remove(FuXings).RemoveWrappings().RemovePunctuations().SplitChars();

            Xings = DanXings.Append(FuXings);

            var mingsText = ResourceUtil.GetTextFromCommon("滕王阁序.txt");
            mingsText = mingsText.RemovePunctuations().RemoveWrappings();
            Mings = mingsText.SplitChars();

            var villagesText = ResourceUtil.GetTextFromCommon("村名.txt");
            Villages = villagesText.SplitByCommaAndWrap();
        }

        public static string GetRandomXing()
        => Xings.Get(RandomUtil.Get(Xings.Length));

        public static string GetRandomMing()
        {
            int mingLength = RandomUtil.Get(2) + 1;//1/2
            var random = RandomUtil.Get(Mings.Length - (mingLength - 1));
            var ming = new StringBuilder();
            mingLength.For(index => ming.Append(Mings.Get(random + index)));
            return ming.ToString();
        }

        public static string RandomVillage()
        => Villages[RandomUtil.Get(Villages.Length)];

        public static string RandomAddress()
        => RandomVillage() + RandomUtil.Get(100) + "号";
    }
}